import sun.font.FontRunIterator;
import sun.security.util.Password;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        //创建集合
        ArrayList<User> list = new ArrayList();
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("****** 1 登录 2 注册 3 忘记密码 4 退出系统 ******");
            System.out.println("请选择");
            Scanner sc = new Scanner(System.in);
            int input = sc.nextInt();
            switch (input) {
                case 1:
                    Login(list);
                    break;
                case 2:
                    Register(list);
                    break;
                case 3:
                    Foreignpassword(list);
                    break;
                case 4: {
                    System.out.println("成功退出");
                    System.exit(0);
                }
                default:
                    System.out.println("输入错误，重新输入");
                    break;
            }
        }
    }

    private static void Foreignpassword(ArrayList<User> list)  {
        //忘记密码修改
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contains(list, username);
        if (!flag) {
            System.out.println("当前用户" + username + "未注册，请先注册");
            return;
        }

        //录入身份证号码和手机号码
        System.out.println("请输入身份证号码");
        String personID = sc.next();
        System.out.println("请输入手机号码");
        String phoneNumber = sc.next();

        //把用户对象通过索引获取出来
        int index = findindex(list, username);
        User user = list.get(index);
        //比较用户对象中的手机号码和身份证号码是否正确
        if (!(user.getPersonid().equalsIgnoreCase(personID) && user.getPhonenumber().equals(phoneNumber))) {
            System.out.println("身份证号码或者手机号码有误不能修改密码");
            return;
        }

        //代码执行到这里代表可以直接修改
        String password;
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("再次输入密码");
            String againpassword = sc.next();
            if (password.equals(againpassword)) {
                System.out.println("两次密码输入一致");
                break;
            } else {
                System.out.println("两次密码输入不一致");
                continue;
            }
        }

        //直接修改即可
        user.setPassword(password);
        System.out.println("密码修改成功");
    }


    private static int findindex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    private static void Register(ArrayList<User> list) {
        //注册
        //1 键盘输入用户名
        Scanner sc = new Scanner(System.in);
        String username;
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();

            //检查是否唯一
            boolean flag1 = checkusername(username);
            if (!flag1) {
                System.out.println("格式错误");
                continue;
            }

            //判断用户名是否存在
            boolean flag2 = contains(list, username);
            if (flag2) {
                //用户名已经存在
                System.out.println("用户名" + username + "已经存在");
            } else {
                System.out.println("用户名" + username + "可用");
                break;
            }
        }

        //2 键盘录入密码
        String againpassword;
        while (true) {
            System.out.println("请输入密码");
            String password = sc.next();
            System.out.println("请再次输入密码");
            againpassword = sc.next();
            if (!(password.equals(againpassword))) {
                System.out.println("密码不一致，请重新输入");
                continue;
            } else {
                System.out.println("两次密码一致，请继续录入其他数据");
                break;
            }
        }

        //3 键盘录入身份证号码
        //长度18位
        //不能以0开头
        String personID;
        while (true) {
            System.out.println("请输入身份证号码");
            personID = sc.next();
            boolean flag = checkpersonid(personID);
            if (flag) {
                System.out.println("身份证号码正确");
                break;
            } else {
                System.out.println("身份证号码错误");
                continue;
            }
        }

        //4 键盘录入手机号码
        //长度11
        //不能以开头
        //必须是数字
        String PhoneNumber;
        while (true) {
            System.out.println("请输入手机号码");
            PhoneNumber = sc.next();
            boolean flag = checkPhoneNumber(PhoneNumber);
            if (flag) {
                System.out.println("手机号码正确");
                break;
            } else {
                System.out.println("手机号码错误");
                continue;
            }
        }

        //用户名 密码 身份证号码都放到一个用户对象中再添加到集合
        User u = new User(username, againpassword, personID, PhoneNumber);
        list.add(u);
        System.out.println("添加成功");

        //进行遍历集合
        printlist(list);
    }

    private static void printlist(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUsername() + "," + user.getPassword() + ","
                    + user.getPersonid() + "," + user.getPhonenumber());
        }
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        //长度11
        if (phoneNumber.length() != 11) {
            return false;
        }
        //不能以0开头
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        //必须是数字
        for (int i = 0; i < phoneNumber.length() - 1; i++) {
            char c = phoneNumber.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        //程序走到这里代表所有条件都满足
        return true;
    }

    private static boolean checkpersonid(String personID) {
        //长度18位
        if (personID.length() != 18) {
            return false;
        }
        //不能以0开头
        if (personID.startsWith("0")) {
            return false;
        }
        //前17位必须是数字
        for (int i = 0; i < personID.length() - 1; i++) {
            char c = personID.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        //最后一位可以是数字也可以是X或者x
        char endChar = personID.charAt(personID.length() - 1);
        if ((endChar >= '0' && endChar <= '9') || (endChar == 'X') || (endChar == 'x')) {
            return true;
        } else {
            return false;
        }

    }

    private static boolean contains(ArrayList<User> list, String username) {
        //遍历查找

        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            String rightusername = user.getUsername();
            if (rightusername.equals(username)) {
                return true;
            }
        }
        //全部为假
        return false;
    }

    private static void Login(ArrayList<User> list) {
        //登录系统
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String username = sc.next();
            //判断是否存在
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户名" + username + "未注册，请先注册在登录");
            }

            //输入密码
            System.out.println("请输入密码");
            String password = sc.next();

            //验证码校验
            while (true) {
                String rightCode = getCode();
                System.out.println("正确的验证码是" + rightCode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (code.equalsIgnoreCase(rightCode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误，请重新输入");
                    continue;
                }
            }


            //用户名和密码是否正确
            //集合中是否包含用户名和密码
            //定义方法检验用户名和密码是否正确

            //封装思想的一个应用
            //将一些零散的数据封装成一个对象
            //传递数据的时候只需要传递一个整体就可以了 不需要传递一个零散的数据
            User userinfo = new User(username, password, null, null);
            boolean result = checkuserinfo(list, userinfo);
            if (flag) {
                System.out.println("登录成功，可以开始使用学生管理系统");
                break;
            } else {
                System.out.println("登录失败，用户名或者密码错误");
                if (i == 2) {
                    System.out.println("用户名" + username + "已经被锁定请联系客服");
                } else {
                    System.out.println("输入错误您还剩下" + (2 - i) + "次机会");
                }
            }
        }
    }

    private static boolean checkuserinfo(ArrayList<User> list, User userinfo) {
        //判断用户名和密码是否正确
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(userinfo.getUsername()) && user.getPassword().equals(user.getPassword())) {
                return true;
            }
        }
        return false;
    }

    private static boolean checkusername(String username) {
        //1 长度在3到15之间
        int len = username.length();
        //判断
        if (len < 3 || len > 15) {
            return false;

        }

        //2 如果有一个字符不是字符或者数字就返回false
        for (int i = 0; i < len; i++) {
            char c = username.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }
        //3 不可以是纯数字必须要有字母  返回有多少个字母
        //记录字母的数量
        int count = 0;
        for (int i = 0; i < len; i++) {
            char c = username.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }


    public static String getCode() {
        //创建集合添加元素
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }

        //创建随机变量
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }

        //添加一个随机数到末尾
        int number = r.nextInt(10);//范围0到9
        sb.append(number);

        //修改字符串中的内容要将其转换成字符数组在进行修改
        char arr[] = sb.toString().toCharArray();
        //拿着最大的索引进行一个交换
        int randomindex = r.nextInt(arr.length);
        char temp = arr[randomindex];
        arr[randomindex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        return new String(arr);
    }
}
